import 'package:financing_wallet/login_page/constant.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/dashLine.dart';
import 'package:wallet_utils/el_refresh.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/ht_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/ht_common_widgets.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:wallet_utils/webview_common.dart';
import 'logic.dart';

class AuditRejectionPage extends StatelessWidget {
  final logic = Get.put(AuditRejectionLogic());
  final state = Get.find<AuditRejectionLogic>().state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        if (logic.popUpsInfo.isNotEmpty) {
          buildShowModalBottomSheet(context);
        } else {
          Get.back();
          EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.popStatus, value: false));
          Get.until((route) => (route.settings.name == BaseRouterConfig.main));
        }
        Get.offAllNamed(BaseRouterConfig.main);
        return Future.value(true);
      },
      child: Scaffold(
        backgroundColor: ColorManager.colorF4F5F6,
        appBar: htCommonAppbar(context, title: '', leadingAction: () {
          if (logic.popUpsInfo.isNotEmpty) {
            buildShowModalBottomSheet(context);
          } else {
            Get.back();
            EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.popStatus, value: false));
            Get.until((route) => (route.settings.name == BaseRouterConfig.main));
          }
        }),
        body: GetBuilder<AuditRejectionLogic>(builder: (logic) {
          return elRefresh(
            height: 50.w,
            refreshController: state.refreshController,
            enablePullUp: false,
            onRefresh: () async {
              logic.obtainAuditResultInfo();
            },
            child: Container(
              color: ColorManager.colorF7F8F9,
              padding: EdgeInsets.only(left: 15.w, right: 15.w, top: 15.w),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Image.asset(
                    'images/auth_reco_logo.png',
                    width: 44.w,
                    height: 10.w,
                  ),
                  Container(
                    height: 276.w,
                    width: double.infinity,
                    margin: EdgeInsets.only(top: 15.w),
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [
                          ColorManager.colorD93C3A,
                          ColorManager.colorFBB9B5,
                        ],
                      ),
                      borderRadius: BorderRadius.all(Radius.circular(8.w)),
                    ),
                    child: Column(
                      children: [
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: EdgeInsets.only(top: 20.w, bottom: 6.w, left: 20.w),
                          child: Text(
                            '审核未通过,为您推荐以下产品',
                            style: TextStyleUtils.styleFFFFFF(fontSize: 16.sp, fontWeight: FontWeight.bold),
                          ),
                        ),
                        // Container(
                        //   alignment: Alignment.centerLeft,
                        //   margin: EdgeInsets.only(left: 20.w),
                        //   child: Opacity(
                        //     opacity: 0.6,
                        //     child: Text(
                        //       '您的借款将由合作方${state.productName}提供',
                        //       style: TextStyleUtils.styleFFFFFF(fontSize: 12.sp),
                        //     ),
                        //   ),
                        // ),
                        Opacity(
                          opacity: 0.29,
                          child: Container(
                            height: 1.w,
                            margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 10.w),
                            child: DashLine(
                              color: ColorManager.colorFFFFFF,
                            ),
                          ),
                        ),
                        Container(
                            margin: EdgeInsets.only(top: 20.w),
                            child: Text(
                              '您的预估额度(元)',
                              style: TextStyleUtils.styleFFFFFF(fontSize: 14.sp),
                            )),
                        Text(
                          state.loanAmount ?? '',
                          style: TextStyleUtils.styleFFFFFF(fontSize: 40.sp, fontWeight: FontWeight.bold),
                        ),
                        SizedBox(
                          height: 5.w,
                        ),
                        Container(
                          margin: EdgeInsets.symmetric(horizontal: 30.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              cashInfoWidget('最快10分钟到账'),
                              cashInfoWidget('月利率${state.loanMinRate}%'),
                              cashInfoWidget('最长可借${state.loanPeriod}期'),
                            ],
                          ),
                        ),
                        ClickWidget(
                          onTap: () {
                            logic.popover(1,0);
                          },
                          child: Container(
                            width: double.infinity,
                            height: 48.w,
                            alignment: const Alignment(0, 0),
                            margin: EdgeInsets.only(right: 20.w, left: 20.w, top: 17.w),
                            decoration: const BoxDecoration(
                              color: Colors.white,
                              //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                              borderRadius: BorderRadius.all(Radius.circular(5.0)),
                            ),
                            child: Text(
                              '立即提现',
                              style: TextStyleUtils.styleFFECE9(fontSize: 18.sp, fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: 65.w,
                    color: ColorManager.colorF4F5F6,
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '热门产品',
                      style: TextStyleUtils.style000000(fontSize: 18.sp, fontWeight: FontWeight.bold),
                    ),
                  ),
                  MediaQuery.removePadding(
                      context: context,
                      removeTop: true,
                      child: Container(
                        color: ColorManager.colorF4F5F6,
                        child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: logic.productList!.isNotEmpty ? logic.productList!.length - 1 : 0,
                          physics: const NeverScrollableScrollPhysics(),
                          itemBuilder: ((context, index) {
                            return productItem(index + 1);
                          }),
                        ),
                      )),
                ],
              ),
            ),
          );
        }),
      ),
    );
  }

  Container cashInfoWidget(String name) {
    return Container(
      decoration: BoxDecoration(
        color: ColorManager.colorFFECE9,
        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
        borderRadius: BorderRadius.all(Radius.circular(3.0)),
      ),
      child: Text(
        name,
        style: TextStyleUtils.styleD93C3A(fontSize: 12.sp),
      ),
      padding: EdgeInsets.only(left: 6.w, right: 6.w, top: 2.w, bottom: 2.w),
    );
  }

  Container productItem(int index) {
    return Container(
        height: 100.w,
        margin: EdgeInsets.only(
          bottom: 16.w,
        ),
        decoration: BoxDecoration(
          color: ColorManager.colorFFFFFF,
          //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
        ),
        child: Container(
          margin: EdgeInsets.only(top: 15.w, left: 14.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  HtImage(icon: true, url: logic.productList![index].logo, width: 18.w, height: 18.w, radius: 2),
                  SizedBox(
                    width: 5.w,
                  ),
                  Text(
                    logic.productList![index].productName ?? '',
                    style: TextStyleUtils.style000000(fontSize: 14.sp, fontWeight: FontWeight.bold),
                  )
                ],
              ),
              SizedBox(
                height: 12.w,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        logic.productList![index].loanAmount ?? '',
                        style: TextStyleUtils.styleF73326(
                          fontSize: 18.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        '最高额度(元)',
                        style: TextStyleUtils.style999999(fontSize: 12.sp),
                      )
                    ],
                  ),
                  Column(
                    children: [
                      Text(
                        '${logic.productList![index].loanPeriod}月',
                        style: TextStyleUtils.style000000(
                          fontSize: 18.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        '借款期限',
                        style: TextStyleUtils.style999999(fontSize: 12.sp),
                      )
                    ],
                  ),
                  ClickWidget(
                    onTap: () {
                      logic.popover(2,index);
                    },
                    child: Container(
                      height: 30.w,
                      padding: EdgeInsets.only(left: 19.w, right: 19.w),
                      alignment: const Alignment(0, 0),
                      margin: EdgeInsets.only(
                        right: 15.w,
                      ),
                      decoration: BoxDecoration(
                          color: ColorManager.colorFFFFFF,
                          //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                          borderRadius: BorderRadius.all(Radius.circular(25.w)),
                          border: Border.all(width: 1.5.w, color: ColorManager.colorD93C3A)),
                      child: Text(
                        '立即申请',
                        style: TextStyleUtils.styleFFECE9(fontSize: 12.sp, fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ));
  }

  Future<dynamic> buildShowModalBottomSheet(BuildContext context) {
    return showModalBottomSheet(
        context: context,
        isDismissible: false,
        isScrollControlled: false,
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
        builder: (BuildContext buildContext) {
          return Container(
            height: 470.w,
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 15.w, left: 16.w, bottom: 20.w),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      'images/icon_complete.png',
                      width: 20.w,
                      height: 20.w,
                    ),
                    SizedBox(
                      width: 10.w,
                    ),
                    Text(
                      '恭喜您，已通过初审',
                      style: TextStyleUtils.style000000(fontSize: 16.sp, fontWeight: FontWeight.bold),
                    ),
                  ],
                ),
                Container(
                  height: 105.w,
                  width: double.infinity,
                  margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 10.w),
                  decoration: BoxDecoration(
                    color: ColorManager.colorFFFFFF,
                    //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                    borderRadius: BorderRadius.all(Radius.circular(5.0)),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        '${state.amount}元',
                        style: TextStyleUtils.styleFFECE9(fontSize: 32.sp, fontWeight: FontWeight.bold),
                      ),
                      Text(
                        '您的借款将由合作方${state.name}提供',
                        style: TextStyleUtils.style666666(fontSize: 12.sp),
                      )
                    ],
                  ),
                ),
                Container(
                  width: double.infinity,
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(
                    left: 15.w,
                    right: 15.w,
                    bottom: 15.w,
                  ),
                  padding: EdgeInsets.only(left: 8.w, right: 8.w, top: 10.w, bottom: 10.w),
                  decoration: BoxDecoration(
                    color: ColorManager.colorF8F8F8,
                    //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                    borderRadius: BorderRadius.all(Radius.circular(5.0)),
                  ),
                  height: 150.w,
                  child: MediaQuery.removePadding(
                    removeTop: true,
                    context: Get.context!,
                    child: ListView(
                      shrinkWrap: true,
                      children: [
                        HtmlWidget(
                          state.agreementContent ?? '',
                        ),
                      ],
                    ),
                  ),
                ),
                ClickWidget(
                  onTap: () {
                    logic.popover(3,0);
                  },
                  child: Container(
                    width: double.infinity,
                    height: 48.w,
                    alignment: const Alignment(0, 0),
                    margin: EdgeInsets.only(right: 16.w, left: 16.w, top: 7.w),
                    decoration: BoxDecoration(
                      color: ColorManager.colorD93C3A,
                      //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                      borderRadius: BorderRadius.all(Radius.circular(25.0)),
                    ),
                    child: Text(
                      '申请提现',
                      style: TextStyleUtils.styleFFFFFF(fontSize: 16.sp, fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
                ClickWidget(
                  onTap: () {
                    Get.back();
                    EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.popStatus, value: false));
                    Get.until((route) => (route.settings.name == BaseRouterConfig.main));
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: 20.w),
                    child: Text(
                      '放弃借款',
                      style: TextStyleUtils.style666666(fontSize: 16.sp, fontWeight: FontWeight.bold),
                    ),
                  ),
                )
              ],
            ),
          );
        });
  }
}
